package com.damon;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.damon.entity.PrintDeviceEntity;
import com.damon.log.WcsLog;
import com.damon.thread.PrintSocketThread;
import com.damon.utils.DbUtil;
import com.damon.utils.GlobalOjbectUtil;
import com.damon.utils.LogUtil;
import com.damon.utils.ThreadPoolUtil;
import com.damon.window.WcsWindow;

public class WcsPrinterRun {
//	static Logger logger = LoggerFactory.getLogger(WcsPrinterRun.class);
//	public static Map<String, Queue<String>> addressMap = new HashMap<>();
	public static void main(String[] args) {
		//实例化可视化界面
		WcsWindow wcsWindow = new WcsWindow();
		GlobalOjbectUtil.setWindow(wcsWindow);
		//加载日志
		WcsLog logger = LogUtil.getLogger(WcsPrinterRun.class);
		logger.info("正在检测待连接设备。。。。。。。。");
		String sql = "select id,device_number,ip,port from print_device where is_connection=1";
		Connection connection = DbUtil.getConnection();
		Statement statement = null;
		ResultSet resultSet;
		try {
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			while(resultSet.next()) {
				PrintDeviceEntity deviceInfo = new PrintDeviceEntity();
				deviceInfo.setId(resultSet.getInt("id"));
				deviceInfo.setDeviceNumber(resultSet.getString("device_number"));
				deviceInfo.setIp(resultSet.getString("ip"));
				deviceInfo.setPort(resultSet.getInt("port"));
				GlobalOjbectUtil.getDeviceList().add(deviceInfo);
			}
		} catch (SQLException e) {
			logger.error("查询设备表出错！！");
			e.printStackTrace();
		}finally {
			try {
				statement.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		//为每个设备创建连接
		logger.info("已检测到："+GlobalOjbectUtil.getDeviceList().size()+" 个待连接设备，开始连接！");
		for(PrintDeviceEntity p : GlobalOjbectUtil.getDeviceList()) {
			ThreadPoolUtil.getExecutor().execute(new PrintSocketThread(p));
		}
		
		
		
	
		
		
	}
	
}
